$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/optionval/list',
        datatype: "json",
        colModel: [			
			{ label: 'ID', name: 'id_', width: 45, key: true,hidden:true },
			{ label: '题干ID', name: 'topicId', width: 90 },
			{ label: '题干选项', name: 'value', width: 90 },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});


$(document).ready(function() {  
	  
	var MaxInputs       = 5; //maximum input boxes allowed  
	var InputsWrapper   = $("#InputsWrapper"); //Input boxes wrapper ID  
	var AddButton       = $("#AddMoreFileBox"); //Add button ID  
	  
	var x = InputsWrapper.length; //initlal text box count  
	var FieldCount=1; //to keep track of text box added  
	  
	$(AddButton).click(function (e)  //on add input button click  
	{  
	        if(x <= MaxInputs) //max input box allowed  
	        {  
	            FieldCount++; //text box added increment  
	            //add input box  
	            $(InputsWrapper).append('<div><input type="text" class="form-control" name="mytext[]" v-model="optionval.value'+ FieldCount + '" placeholder="题干选项" /><a href="#" class="removeclass">×</a>');  
	            x++; //text box increment  
	        }  
	return false;  
	});  
	  
	$("body").on("click",".removeclass", function(e){ //user click on remove text  
	        if( x > 1 ) {  
	                $(this).parent('div').remove(); //remove text box  
	                x--; //decrement textbox  
	        }  
	return false;  
	})   
	  
	}); 

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			
		},
		showList: true,
		title:null,
		optionval:{
			
		},
		questionList:[

			]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.roleList = {};
			vm.optionval = {};
			vm.queryquestionList();
		},
		queryquestionList: function () {//查询
		
			$.ajax({
				type: "POST",
			    url: baseURL + "sys/optionval/queryquestionList",
                contentType: "application/json",
			    data: JSON.stringify(),
			    success: function(r){
					if(r.code == 0){
						vm.questionList=r.questionList;
						$("#question").val(r.questionList);
					}else{
						alert(r.msg);
					}
				}
			});
		},
		
		update: function () {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			
			vm.showList = false;
            vm.title = "修改";
            vm.queryquestionList();
			vm.getOptionval(userId);
		},
		del: function () {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/optionval/delete",
                    contentType: "application/json",
				    data: JSON.stringify(userIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
			var url = vm.optionval.id_ == null ? "sys/optionval/save" : "sys/optionval/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.optionval),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getOptionval: function(userId){
			$.get(baseURL + "sys/optionval/info/"+userId, function(r){
				vm.optionval = r.optionval;
			//	vm.optionval.passWord = null;

              
			});
		},
		
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'name': vm.q.value},
                page:page
            }).trigger("reloadGrid");
		}
	}
});